package com.oanda.blackberry;

import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.UiApplication;

import com.oanda.blackberry.net.AutoConnectionManager;
import com.oanda.blackberry.util.DefaultController;
import com.oanda.blackberry.util.Model;
import com.oanda.blackberry.util.Settings;
import com.oanda.blackberry.view.AccountDetails;
import com.oanda.blackberry.view.AccountStatusView;
import com.oanda.blackberry.view.AccountsView;
import com.oanda.blackberry.view.HistoryView;
import com.oanda.blackberry.view.LoginView;
import com.oanda.blackberry.view.NewOrderView;
import com.oanda.blackberry.view.NewTradeView;
import com.oanda.blackberry.view.OptionsView;
import com.oanda.blackberry.view.OrderDetailsView;
import com.oanda.blackberry.view.OrdersView;
import com.oanda.blackberry.view.OtpHelpView;
import com.oanda.blackberry.view.OtpView;
import com.oanda.blackberry.view.RatesSymbolsView;
import com.oanda.blackberry.view.RatesView;
import com.oanda.blackberry.view.RegistrationView;
import com.oanda.blackberry.view.ScreenViewController;
import com.oanda.blackberry.view.StatusView2;
import com.oanda.blackberry.view.TradesDetailsView;
import com.oanda.blackberry.view.TradesView;

public class OandaBlackBerryApplication extends UiApplication {
    public static boolean isInForeground;
    private HistoryView vHistory;
    private NewTradeView vNewTrade;
    private RatesView vRates;
    private OrdersView vOrders;
    private TradesView vTrades;

    public static void main(String[] args) {
        OandaBlackBerryApplication theApp = new OandaBlackBerryApplication();
        theApp.enterEventDispatcher();
    }

    public OandaBlackBerryApplication() {
        Settings settings = Settings.getSettings();
        Model model = Model.getModel();

        DefaultController controller = new DefaultController();

        LoginView loginView = new LoginView(controller);
        AccountsView accouts = new AccountsView(controller);
        AccountStatusView accoutsStatus = new AccountStatusView(controller);
        StatusView2 status = new StatusView2(controller, controller);
        OptionsView options = new OptionsView(controller);
        ScreenViewController screenViewC = new ScreenViewController(controller);
        RatesSymbolsView symbolsView = new RatesSymbolsView(controller);
        RatesView ratesView = new RatesView(controller);
        NewTradeView newTradeView = new NewTradeView(controller);
        HistoryView historyView = new HistoryView(controller);
        TradesView tradesView = new TradesView(controller);
        TradesDetailsView tradesDetailsView = new TradesDetailsView(controller);
        NewOrderView newOrderView = new NewOrderView(controller);
        OrdersView ordersView = new OrdersView(controller);
        OrderDetailsView orderDetailsView = new OrderDetailsView(controller);
        AccountDetails accountDetails = new AccountDetails(controller);
        RegistrationView registrationView = new RegistrationView(controller);
        OtpView otpView = new OtpView(controller);
        OtpHelpView otpHelpView = new OtpHelpView(controller);

        controller.addView(loginView);
        controller.addView(status);
        controller.addView(accouts);
        controller.addView(accoutsStatus);
        controller.addView(options);
        controller.addView(screenViewC);
        controller.addView(symbolsView);
        controller.addView(ratesView);
        controller.addView(newTradeView);
        controller.addView(historyView);
        controller.addView(tradesView);
        controller.addView(tradesDetailsView);
        controller.addView(newOrderView);
        controller.addView(ordersView);
        controller.addView(orderDetailsView);
        controller.addView(accountDetails);
        controller.addView(registrationView);
        controller.addView(otpView);
        controller.addView(otpHelpView);

        controller.addModel(model);

        model.setSettings(settings);

        AutoConnectionManager conManager = AutoConnectionManager.getInstance();
        CoverageInfo.addListener(conManager);
        WLANInfo.addListener(conManager);

        controller.addView(conManager);

        controller.show();

        setHistoryView(historyView);
        setNewTradeView(newTradeView);
        setRatesView(ratesView);
        setOrdersView(ordersView);
        setTradesView(tradesView);
    }

    public static boolean isSimulator() {
        return DeviceInfo.isSimulator();
    }

    public void activate() {
        isInForeground = true;
        this.vHistory.notifyTimerTask();
        this.vNewTrade.notifyTimerTask();
        this.vRates.notifyTimerTask();
        this.vOrders.notifyTimerTask();
        this.vTrades.notifyTimerTask();
    }

    public void deactivate() {
        isInForeground = false;
    }

    public void setHistoryView(HistoryView hView) {
        this.vHistory = hView;
    }

    public void setNewTradeView(NewTradeView ntView) {
        this.vNewTrade = ntView;
    }

    public void setRatesView(RatesView rView) {
        this.vRates = rView;
    }

    public void setOrdersView(OrdersView oView) {
        this.vOrders = oView;
    }

    public void setTradesView(TradesView tView) {
        this.vTrades = tView;
    }
}
